package cn.anline.ann_wp

import android.app.Activity
import android.database.Cursor
import android.os.Bundle
import android.os.PersistableBundle
import android.provider.MediaStore
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.Button
import android.widget.Switch
import android.widget.Toast
import cn.anline.ann_wp.adapter.BaseAdapterRV
import cn.anline.ann_wp.adapter.SelectorVideoAdapter
import cn.anline.ann_wp.api.VideoWallpaper
import cn.anline.ann_wp.bean.VideoInfo
import cn.anline.ann_wp.utils.SPUtil
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.functions.Action1
import rx.functions.Func1
import rx.schedulers.Schedulers
import java.io.File
import java.util.ArrayList

/**
 * Created by luozh on 2017/7/12.
 */
class MainActivity : Activity() {
    private var mRecyclerView: RecyclerView? = null
    private val mVideoInfoList = ArrayList<VideoInfo>()
    private var mAdapter: SelectorVideoAdapter? = null
    private var mBtn_ok: Button? = null
    private var mVideoInfo: VideoInfo? = null
    private var mSwitch: Switch? = null

    private var mFile1: File? = null
    private var mFile2: File? = null
    private var mPath:String? = ""
    private var mVideoWallpaper: VideoWallpaper? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mRecyclerView = this.findViewById<RecyclerView>(R.id.rv_selector_video)
        mBtn_ok = findViewById<Button>(R.id.btn_ok)
        mSwitch = findViewById(R.id.v_)
        mVideoWallpaper = VideoWallpaper()

        val linearLayoutManager = GridLayoutManager(this, 3)
        (mRecyclerView as RecyclerView?)!!.setLayoutManager(linearLayoutManager)
        mAdapter = SelectorVideoAdapter()
        (mRecyclerView as RecyclerView?)!!.setAdapter(mAdapter)
        mAdapter!!.setItemOnClickListener { position, view, t ->
            val info:VideoInfo = t as VideoInfo
            mAdapter!!.setSelectorPosition(position)
            if (mAdapter!!.getSelectorPosition() !== -1) {
                mVideoInfo = info
                mBtn_ok!!.setText("立即设为视频背景")
                mPath = info.path
            } else {
                mVideoInfo = null
                mPath = ""
                mBtn_ok!!.setText("请选择MP4视频")
            }
        }
        mBtn_ok!!.setOnClickListener {
            if (mPath!!.endsWith(".mp4") || mPath!!.endsWith(".MP4")){
                mVideoWallpaper!!.setToWallPaper(this, mPath!!)
                if (mSwitch!!.isChecked){
                    VideoWallpaper.setVoiceNormal(this)
                    Toast.makeText(this,"有声模式设置了视频背景",Toast.LENGTH_SHORT).show()
                }else{
                    VideoWallpaper.setVoiceSilence(this)
                    Toast.makeText(this,"静音模式设置了视频背景",Toast.LENGTH_SHORT).show()
                }
            }else{
                Toast.makeText(this,"请选择mp4类型的视频",Toast.LENGTH_LONG).show()
            }
        }

        mSwitch!!.setOnCheckedChangeListener { compoundButton, b ->
            if (b){
                VideoWallpaper.setVoiceNormal(this)
                Toast.makeText(this,"视频背景音乐已开启",Toast.LENGTH_SHORT).show()
            }else{
                VideoWallpaper.setVoiceSilence(this)
                Toast.makeText(this,"视频背景音乐已关闭",Toast.LENGTH_SHORT).show()
            }
        }

    }

    /**
     * 获取本地视频资源是耗时操作！！！！可以不使用rxjava 线程中操作也可以
     */
    private fun getVideoInfoList() {
        val cursor = this.getContentResolver().query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, null) ?: return

        Observable.just<Cursor>(cursor)
                .map { cursor -> cursorToList(cursor) }
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ videoInfos ->
                    mAdapter!!.addData(videoInfos)

                    mAdapter!!.notifyDataSetChanged()
                }) { throwable -> throwable.printStackTrace() }
    }

    /**
     * 将扫描的视频添加到集合中
     * @param cursor
     * *
     * @return
     */
    private fun cursorToList(cursor: Cursor): List<VideoInfo> {

        mVideoInfoList.clear()
        var videoInfo: VideoInfo
        while (cursor.moveToNext()) {
            val id = cursor.getInt(cursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media._ID))
            val title = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE))
            val album = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM))

            val artist = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST))
            val displayName = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME))
            val mimeType = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE))
            val path = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.DATA))
            val duration = cursor
                    .getInt(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)).toLong()
            val size = cursor
                    .getLong(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE))

            videoInfo = VideoInfo(id, title, album, artist, displayName,
                    mimeType, path, size, duration)
            mVideoInfoList.add(videoInfo)
        }
        cursor.close()

        return mVideoInfoList
    }

    public override fun onResume() {
        super.onResume()
        getVideoInfoList()
    }


    fun setWallpaper(view: View) {

    }

    fun setSilence(view: View) {
        VideoWallpaper.setVoiceSilence(this)
    }

    fun cancelSilence(view: View) {
        VideoWallpaper.setVoiceNormal(this)
    }

    fun toBack(view: View) {
        finish()
    }

}